package yunquan.wang.test.importData;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Iterator;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.junit.Test;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.modules.base.dao.CodeValueDao;
import yunquan.wang.modules.base.entity.CodeType;
import yunquan.wang.modules.base.entity.CodeValue;
import yunquan.wang.modules.base.service.AppCodeService;
import yunquan.wang.test.ServiceTest;

public class ImportTrade extends ServiceTest{
@Resource
	private   AppCodeService appCodeService;
@Resource
private CodeValueDao codeValueDao;
 	@Test
 	@Transactional("jpaTransaction")
 @Rollback(false)
	public void importData() throws Exception
	{
		BufferedInputStream bf =new BufferedInputStream( new FileInputStream( "D:\\表.xls"));  
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(bf);   
		 HSSFSheet sheet=	   hssfWorkbook.getSheetAt(0);
		 Iterator<Row>  rows=	   sheet.rowIterator();
		   CodeType codeType=new CodeType();
		   codeType.setEditable(false);
		   codeType.setName("行业分类");
		   codeType.setType("tradetype.classify");
		   codeType.setValueType("string");
		   appCodeService.save(codeType);
		   
		   CodeType tradeBig=new CodeType();
		   tradeBig.setEditable(false);
		   tradeBig.setName("行业大类");
		   tradeBig.setType("tradetype.big");
		   tradeBig.setValueType("string");
		   tradeBig.setParent(codeType);
		   appCodeService.save(tradeBig);
		   
		   CodeType trademiddle=new CodeType();
		   trademiddle.setEditable(false);
		   trademiddle.setName("行业中类");
		   trademiddle.setType("tradetype.middle");
		   trademiddle.setValueType("string");
		   trademiddle.setParent(tradeBig);
		   appCodeService.save(trademiddle);
		   
		   
		   CodeType tradesmall=new CodeType();
		   tradesmall.setEditable(false);
		   tradesmall.setName("行业小类");
		   tradesmall.setType("tradetype.small");
		   tradesmall.setValueType("string");
		   tradesmall.setParent(trademiddle);
		   appCodeService.save(tradesmall);
		  
		   rows.next();
		   rows.next();
		   int i=0;
		   while(rows.hasNext())
		   {
			   i++;
			   Row row=  rows.next();
			String one=   ExcelUitls.getCellValue(row, 0);
			if(one.length()>0)
			{
				   CodeValue codeValue=new CodeValue();
				   codeValue.setCode(one);
				   codeValue.setEnable(true);
				   codeValue.setStringValue(ExcelUitls.getCellValue(row, 4));
				   codeValue.setCodeType(codeType);
					codeValueDao.save(codeValue);
			}
			
			String two=   ExcelUitls.getCellValue(row,1);
			if(two.length()>0)
			{
				   CodeValue codeValue=new CodeValue();
				   codeValue.setCode(two);
				   codeValue.setEnable(true);
				   codeValue.setStringValue(ExcelUitls.getCellValue(row, 4));
				   codeValue.setCodeType(tradeBig);
					codeValueDao.save(codeValue);
			}
			   
		   
			String three=   ExcelUitls.getCellValue(row, 2);
			if(three.length()>0)
			{
				   CodeValue codeValue=new CodeValue();
				   codeValue.setCode(three);
				   codeValue.setEnable(true);
				   codeValue.setStringValue(ExcelUitls.getCellValue(row, 4));
				   codeValue.setCodeType(trademiddle);
					codeValueDao.save(codeValue);
			}
			   
			String four=   ExcelUitls.getCellValue(row, 3);
			if(four.length()>0)
			{
				   CodeValue codeValue=new CodeValue();
				   codeValue.setCode(four);
				   codeValue.setEnable(true);
				   codeValue.setStringValue(ExcelUitls.getCellValue(row, 4));
				   codeValue.setCodeType(tradesmall);
					codeValueDao.save(codeValue);
			}
		   
	}
 
	}
}
